package com.iptv.grape.bu.imp;

import android.text.TextUtils;
import android.view.KeyEvent;

import com.iptv.grape.BaseActivity;
import com.iptv.grape.bu.AppState;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.bu.IKeyEventPorc;
import com.iptv.grape.domain.Channel;
import com.iptv.grape.event.EventArgs;
import com.iptv.grape.event.data.LiveData;
import com.iptv.grape.util.AppConstant;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.ToastUtil;
import com.iptv.grape.video.XMediaPlayer;

import org.greenrobot.eventbus.EventBus;

// 根据输入的频道号立即换台
@AppState(isChangingLive = true,curPlayType="Live", curLiveType="ALL",keyVals = {66,23},isIgnoreTimeLiveUI = true)
public class StartChangingLiveNow implements IKeyEventPorc {

    @Override
    public void onKeyDownEvent(BaseActivity context, XMediaPlayer mediaPlayer, GrapeContext grapeContext, KeyEvent event) {
        grapeContext.removeChangingLiveHandle();
        int numNo = Integer.parseInt(grapeContext.getCurInputNum());
        Channel channel = grapeContext.getChannelDLinkList().findChannelByNo(numNo);

        String channelID= grapeContext.getChannelDLinkList().getChannelHis().getCurPlayChannel().getChannelID();

        if(channel!=null){
            String keyID =channel.getKeyID();
            if(!TextUtils.isEmpty(channelID)&&channelID.equals(keyID)) {
                ToastUtil.show("当前频道正在播放中");
                context.hideChangingLiveUI();
                grapeContext.setChangingLive(false);
                return;
            }
        }

        if(channel != null){

            LiveData quitLiveData = new LiveData(grapeContext.getChannelDLinkList().getCurChannel(),"2",
                    "23","1");
            EventBus.getDefault().post(new EventArgs(AppConstant.DataLive, AppConstant.DataAcquisition,quitLiveData));
            if( grapeContext.curLiveType().equals("Back")){
                grapeContext.setCurPlayType("Live");
                grapeContext.setCurLiveType("Live");
                grapeContext.removeChannelBillUI(context);
            }
            if( grapeContext.curLiveType().equals("Time")){
                grapeContext.setCurLiveType("Live");
                grapeContext.removeTimeLiveFragment(context);
            }
            channel.playChannel();
            context.hideChangingLiveUI();
            LiveData liveData = new LiveData(channel,"2","23","0");
            EventBus.getDefault().post(new EventArgs(AppConstant.DataLive, AppConstant.DataAcquisition,liveData));
        }else {
            context.NoFindNo();
        }
        grapeContext.setChangingLive(false);
    }
}
